import Base from 'models/base';

export default class UserForm extends Base {
  constructor(user, userInfo) {
    super();

    let branchId = user.branchId;
    let hasAdmin = branchId === 1;
    this.hasAdmin = hasAdmin;
    if (userInfo === undefined) {
      userInfo = {
        status: 1
      };
      if (!hasAdmin) {
        userInfo.branchId = branchId;
        userInfo.branchName = user.branchName;
      }
    } else {
      this.userId = userInfo.userId;
    }

    let fields = ['username', 'realName', 'userPhone', 'branchId', 'branchName', 'partnerId', 'partnerName', 'roleId', 'roleName', 'status'];
    this.initObject(userInfo, fields);
  }

  checkValid() {
    let fields = ['username', 'realName', 'branchId', 'roleId', 'userPhone'];
    return this.checkRequired(fields);
  }

  getObject() {
    let postData = {};
    let fields = ['username', 'realName', 'branchId', 'partnerId', 'roleId', 'userPhone', 'status'];
    this.setPostData(postData, fields);

    let userId = this.userId;
    if (userId !== undefined) {
      postData.userId = userId;
    } else {
      postData.password = '123456';
    }
    return postData;
  }
}
